//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "StockDlg.h"
#include "StockReport.h"
#include "FormUtilities.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
__fastcall TfrmStockDlg::TfrmStockDlg(TComponent* Owner, TDatabase * dbMain)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this); 

   this->dbMain = dbMain;
   bridge = new TGlassesBridge( dbMain);
}

//---------------------------------------------------------------------------

void __fastcall TfrmStockDlg::FormDestroy(TObject *Sender)
{
   delete bridge;
}

//---------------------------------------------------------------------------

void __fastcall TfrmStockDlg::btnProduceClick(TObject *Sender)
{
   TGlassesFirm * firm;
   TGlassesModel * model;
   __int64 id;
   AnsiString name;
   TfrmStockReport * frmStockReport;

   frmStockReport = new TfrmStockReport( this, dbMain);

   try
   {
      if( cmbFirms->ItemIndex == 0)
      {
         id = -1;
         name = "-1";
      }
      else
      {
         firm  = ( TGlassesFirm * )cmbFirms->Items->Objects[ cmbFirms->ItemIndex];
         model = ( TGlassesModel * )cmbModels->Items->Objects[ cmbModels->ItemIndex];

         id = firm->id;

         if ( model == NULL)
            name = "-1";
         else
            name = model->name;
      }

      frmStockReport->Preview( id, name);
   }
   __finally
   {
      delete frmStockReport;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmStockDlg::FormShow(TObject *Sender)
{
   bridge->GetFirms( cmbFirms->Items);
   cmbFirms->Items->InsertObject( 0, "[ALL]", NULL);
   cmbFirms->ItemIndex = 0;

   ShowModels();
}

void TfrmStockDlg :: ShowModels()
{
   TGlassesFirm * firm;

   if ( cmbFirms->ItemIndex == 0)
      bridge->GetModels( -1, cmbModels->Items);
   else
   {
      firm = ( TGlassesFirm * )cmbFirms->Items->Objects[ cmbFirms->ItemIndex];
      bridge->GetModels( firm->id, cmbModels->Items);
   }

   cmbModels->Items->InsertObject( 0, "[ALL]", NULL);
   cmbModels->ItemIndex = 0;
}

//---------------------------------------------------------------------------

void __fastcall TfrmStockDlg::cmbFirmsChange(TObject *Sender)
{
   ShowModels();
}
//---------------------------------------------------------------------------


